package com.phpxiu.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.phpxiu.app.adapter.holder.LiveHistoryViewHolder;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.LiveHistory;
import com.phpxiu.app.utils.KKYUtil;

import java.util.List;

/**
 * 回放列表
 */
public class LiveHistoryListAdapter extends BaseAdapter<LiveHistory> {
    private String dateDistanceYMD;
    private String dateDaysSuffix;
    private String dateHoursSuffix;
    private String dateMinuteSuffix;
    private String dateDefaultSuffix;
    private String defTitle;
    public LiveHistoryListAdapter(Context context, List<LiveHistory> data) {
        super(context, data);
        defTitle=context.getString(R.string.default_history_title);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LiveHistory history=data.get(position);
        LiveHistoryViewHolder holder=null;
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.live_history_list_item_view,null);
            holder=new LiveHistoryViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder=(LiveHistoryViewHolder)convertView.getTag();
        holder.setHistory(history);
        if(history.getTitle()!=null&&!"".equals(history.getTitle())){
            holder.titleView.setText(history.getTitle());
        }else{
            holder.titleView.setText(defTitle);
        }
        holder.watchView.setText(history.getViewed());
        try {
            long distance=Long.parseLong(history.getDate());
            holder.timeView.setText(KKYUtil.timeDistance(distance, dateDistanceYMD, dateDaysSuffix, dateHoursSuffix, dateMinuteSuffix, dateDefaultSuffix));
        }catch (NumberFormatException e){

        }
        if(history.isLive()){
            if(holder.stateView.getVisibility()!=View.VISIBLE){
                holder.stateView.setVisibility(View.VISIBLE);
            }
            String t=history.getType();
            if("3".equals(t)){
                holder.stateView.setImageResource(R.mipmap.home_discover_live_state_icon);
            }else if("2".equals(t)){
                holder.stateView.setImageResource(R.mipmap.home_discover_live_state_icon);
            }else{
                holder.stateView.setImageResource(R.mipmap.home_discover_live_state_icon);
            }
        }else {
            if(holder.stateView.getVisibility()==View.VISIBLE){
                holder.stateView.setVisibility(View.GONE);
            }
        }
        if(data.size()>0&&position==data.size()-1){
            if(holder.dividerView.getVisibility()==View.VISIBLE){
                holder.dividerView.setVisibility(View.INVISIBLE);
            }
        }else {
            if(holder.dividerView.getVisibility()!=View.VISIBLE){
                holder.dividerView.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }

    public void initDateSuffix(String ymd,String d,String h,String m,String defaultStr){
        dateDistanceYMD=ymd;
        dateDaysSuffix=d;
        dateHoursSuffix=h;
        dateMinuteSuffix=m;
        dateDefaultSuffix=defaultStr;
    }
}
